package com.taxigang.client.uibinder;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.Widget;
import com.taxigang.client.Session;
import com.taxigang.client.facebook.FacebookApi;
import com.taxigang.client.facebook.PostCommand;
import com.taxigang.client.facebook.Posting;
import com.taxigang.client.micro.TaxiGangCreator;
import com.taxigang.client.ui.map.MapView.PanelType;
import com.taxigang.client.uibinder.base.CreateStep;
import com.taxigang.shared.TaxiGangInfo;

public class WantToPost extends CreateStep {

	private static WantToPostUiBinder uiBinder = GWT
	.create(WantToPostUiBinder.class);

	interface WantToPostUiBinder extends UiBinder<Widget, WantToPost> {
	}

	@UiField
	Button okButton;

	@UiField
	Button noButton;

	@UiField
	Label headerLabel;

	@UiField
	Label invitationQuestion;

	@UiField TextArea invitationTextArea;

	private boolean completed;
	private String link;
	private PostCommand cmd;
	private String path="/me/feed";
	//private String path="/174406889328525";
	
	private Posting posting;

	public WantToPost() {
		initWidget(uiBinder.createAndBindUi(this));
		bindUI();
		okButton.setText(TaxiGangCreator.taxiGangConstants.post());
		noButton.setText(TaxiGangCreator.taxiGangConstants.dontPost());
		headerLabel.setText(TaxiGangCreator.taxiGangConstants.promote());

	}

	public void show(){
		super.show();
		TaxiGangInfo info = Session.getInstance().getCurrentMyTaxiGang();
		cmd = getCommand(info);
		invitationQuestion.setText(TaxiGangCreator.taxiGangConstants.wantToPostMe());
		invitationTextArea.setText(invitationMessage(info));
		FacebookApi.search(cmd, info);

	}

	private void bindUI() {
		okButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				//previous
				String invitationText = invitationTextArea.getText();
				String postingMessage = "POST "+ path + " "+link+" ";
				if (posting != null){
					postingMessage+=posting.getName();
				}
				Log.info(postingMessage);
				FacebookApi.postTaxigang(path,invitationText,link,FacebookApi.getDumper());	

				posting = prepareNext();
				if (posting !=null){
					path = "/me/feed".replace("me", posting.getId());
				}
			}
		});

		noButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {				
				posting = prepareNext();
			}
		});

	}

	private Posting prepareNext(){
		//prepare for next
		Posting posting = cmd.getNext();
		if (posting == null){
			taxiGangRoute.show(PanelType.MYTAXIGANGS);
		} else {	
			String id = posting.getId();
			invitationQuestion.setText(posting.getQuestion());
		} 		
		return posting;

	}


	private PostCommand getCommand(TaxiGangInfo taxiGangInfo) {
		PostCommand cmd = new PostCommand(){

			@Override
			public void execute() {
				okButton.setEnabled(true);				
			}
		};
		return cmd;
	}

	private String invitationMessage(TaxiGangInfo info){
		link = info.getLink();

		String msgBody = TaxiGangCreator.taxiGangConstants.invitationGreeting()+"\n";;
		msgBody += TaxiGangCreator.taxiGangConstants.from()+":"+info.getStart()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.to()+":"+info.getDestination()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.time()+":"+info.getDepartureTime()+"\n";
		msgBody += TaxiGangCreator.taxiGangConstants.invitationLink()+"\n\n";
		msgBody += link;
		return msgBody;
	}

}
